<?php

class CheckAction extends Action{
	function getIp4check() {
		$limmit = isset($_REQUEST['limit'])?$_REQUEST['limit']:50;
		$limmit = min($limmit,400);
		$model = D('Agent');
		$time = time()-3600*24;//24小时
		$items	=	$model->field('agt_id,agt_ip,agt_port,agt_type,agt_rank,agt_lasttime,agt_err')->where("agt_lasttime + agt_err*3600*24 < $time and agt_rank<4 and agt_country='中国' and agt_province not in ('台湾','香港','澳门') AND (agt_rank>0 AND agt_err<8 OR agt_err < 3)")->order('agt_lasttime + agt_err*3600*24 ASC')->limit($limmit)->select();
		if ($items) {
			echo json_encode($items);
		}else {
			echo json_decode(0);
		}
	}
	function getIp4checkCnt() {
		$model = D('Agent');
		$time = time()-3600*24;//24小时
		$items	=	$model->where("agt_lasttime + agt_err*3600*24 < $time and agt_rank<4 and agt_country='中国' and agt_province not in ('台湾','香港','澳门') AND (agt_rank>0 AND agt_err<8 OR agt_err < 3)")->count();
		if ($items) {
			echo json_encode($items);
		}else {
			echo json_decode(0);
		}
	}
	/**
	 * 表示还可用
	 */
	function alive() {
		$model = D('Agent');
		if (empty($_GET['agt_id'])) {
			;
		}else {
			$time = time();
			$data	=	array(
					'agt_lasttime'	=>	$time,
					'agt_checktime'=>$time,
					'agt_id' => $_GET['agt_id'],
					'agt_err'=>0,
			);
			$model->save($data);
		}
	}
	
	function setRank() {
		$model = D('Agent');
		$get = $_GET;
		$time = time();
		$data	=	array(
				'agt_lasttime'	=>	$time,
				'agt_checktime'=>$time,
				'agt_id' => $get['agt_id'],
		);
		$get['agt_lasttime'] = $time;
		if (isset($get['agt_err'])) {
			$data['agt_err']	=	array('exp','agt_err+1');
			$model->save($data);
		}elseif ($get['agt_rank']){
			$data['agt_err'] = 0;
			$data['agt_rank'] = $get['agt_rank'];
			$model->save($data);
		}
	}
	
	function info() {
		$model = D('Agent');
		$data = array(
			'noAddr'=>	$model->where('agt_country=""')->count(),
			'aliveIp' =>	$model->where('agt_rank in (1,2,3) and agt_err=0')->count('DISTINCT agt_ip'),
		);
		echo json_encode($data);
	}

}
